<?php
namespace App\Models\Account;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

// 玩家 根据游戏角色等级 新增流失率 
class DurationDeinc extends  Model{

	protected $table='player_info';
	public function paginate()
    {
    	$perPage = Request::get('per_page', 100);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;


        $tableAccount = \Config::get('database.connections.mysql_bgsi.prefix').'player_info';
        //$data = \DB::connection($this->selectdb())->table($tableAccount)->skip($page* $perPage)->take($perPage)->get();
        $offset = ($page -1) * $perPage; 
        $sel = "account,lastLoginTime,area,level, count(1) as tot";
        $where = '';
        $cond = self::getCond($where);
        $data = \DB::connection($this->selectdb())->select("select {$sel} FROM {$tableAccount} WHERE 1= 1 {$where} GROUP BY level LIMIT $offset, $perPage", $cond);
        
        
        $newCond = self::getCond($newwhere, 'createTime');
        $newData = \DB::connection($this->selectdb())->select("select {$sel} FROM {$tableAccount} WHERE 1= 1 {$newwhere} GROUP BY level LIMIT $offset, $perPage", $cond);
        $data = self::merge($data, $newData);
        //$data = $data->toArray();

        \Log::debug('duration deinc', [$where, $cond]);
        $total = 100;//\DB::connection($this->selectdb())->table('player_info')->count();

        // $data = json_decode($data, true);

        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }

    public function selectdb(){
        return 'mysql_bgsi';
    }

    public static function merge($data, $newData){
        if (empty($data)) {
            return [];
        }
        $sum = new \stdClass;
    
        $new = [];
        foreach ($newData as $obj) {
            $new[$obj->level]['tot'] = $obj->tot;
        }

        foreach ($data as $obj) {
            $obj->new = $new[$obj->level]['tot'] ?: 0;
            $sum->level = '求和';
            $sum->tot += $obj->tot;
            $sum->new += $obj->new;
        }

        $data[] = $sum;
        return $data;
    }

    public static function getCond(&$where = '', $fld = 'lastLoginTime'){
        
        $cond = [];
        $begin = Request::get('start');
        $end = Request::get('end');
        $area = Request::get('area');

        $level = Request::get('level');

        if (!$begin) {
            $begin = date("Y-m-d", strtotime('-2 day'));
        }
        $end = ($end ?:$begin) . " 23:59:59";
        $begin .= " 00:00:00";
        
        $where .= " AND {$fld} between ? AND ? ";

        $cond[] = $begin;
        $cond[] = $end;

        if ($area) {
            $where .= " AND area IN (?)";
            $cond[] = $area;
        }

        if ($level) {
            $where .= " AND level  <= ? ";
            $cond[] = $level;

        }

        return $cond;
    }
}
